package gui.login;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
 * 
 * manage Confign class
 * all requests to config class are through it
 * 
 * --Singleton--
 * 
 * @author Orit
 *
 */
public class ConfigManager {
	//configuration XML file path
	final String configFile="DBconfig.xml";
	//default server name
	final String defaultHost= "localhost";
	//default server port
	final int defaultPort = 7777;
	//holds the user's configuratoion
	private Config config=null;
	//--Singleton implementation--//	
	private static class ConfigManagerHolder {
		static final ConfigManager configManagerHolder=new ConfigManager();
	}
	
	//constructor
	private ConfigManager() {
		setConfig();
			
	}
	/**
	 * get ConfigManager instance
	 * @return ConfigManager instance
	 */
	public static ConfigManager get(){
		return ConfigManagerHolder.configManagerHolder;
	}
	/**
	 * get config class instance
	 * @return config instance
	 */
	public Config getConfig() {
		return config;
	}
	/**
	 * checks if config file has user information
	 * @return true if config file holds user info, flase OW
	 */
	public boolean hasUserInfo(){
		if (config.getUserName()==null 
				|| config.getPassword()==null){
			return false;
		}
		return true;
	}
	
	public void initConfig(){
		File file=new File(configFile);
		if (file.exists()){
			file.delete();
		}
		config.setUserName(null);
		config.setPassword(null);
	}
	
	/*
	 *set config class 
	 */
	private void setConfig(){
		File file=new File(configFile);
		//check if config file not exists
		if (!file.exists()){
			//build config file
			buildConfig(null,null,defaultHost,defaultPort);
		}
		//get info from config file
		decodeConfig(file);		
	}
	
	/*
	 * decode config file 
	 * @param file config file
	 */
	private void decodeConfig(File file){
		XMLDecoder decoder;
		try {
			//decode config file to config instance
			decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));		
			config = (Config)decoder.readObject();
			decoder.close();
		} 
		catch (FileNotFoundException e) {				
		}
	}
	
	/**
	 * get user name
	 * @return user name
	 */
	public String getUserName(){
		if (config!=null){
			return config.getUserName();
		}		
		return null;
	}
	
	/**
	 * get user's password
	 * @return user's password
	 */
	public String getPassword(){
		if (config!=null){
			return config.getPassword();
		}
		return null;
	}
	
	/**
	 * set user name temporarily (until next time program starts)
	 * @param userName user's name
	 */
	public void setTempName(String userName){		
		if (config!=null){
			config.setUserName(userName);
		}
	}
	
	/**
	 * set user's password temporarily (until next time program starts)
	 * @param password user's password
	 */
	public void setTempPass(String password){
		if (config!=null){
			config.setPassword(password);
		}		
	}
	
	/*
	 * build configuration XML file
	 * @param userName - user's name
	 * @param password - user's password
	 * @param dropBoxPath - user's absolute dropBox path
	 * @param hostName - server's name
	 * @param serverPort - server's port
	 */
	private void buildConfig(String userName,String password,String hostName,int serverPort){
		//set new config
		Config newConfig=new Config();
		newConfig.setPassword(password);
		newConfig.setUserName(userName);
		newConfig.setHostName(hostName);
		newConfig.setServerPort(serverPort);
		
		//encode config instance to XML file
		XMLEncoder encoder;
		try {
			encoder = new XMLEncoder(new BufferedOutputStream(
						new FileOutputStream(new File(configFile))));			
			encoder.writeObject(newConfig);
			encoder.close();
			
		} catch (FileNotFoundException e1) {
		}
		this.config=newConfig;
	     
	}
	
	/**
	 * update configuration file (in case user changed his user name or password)
	 * @param userName the user's name
	 * @param password user's password
	 */
	public void updateConfig(String userName, String password) {
		buildConfig(userName,password,config.getHostName(),config.getServerPort());
		
	}
}
